package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.monitor.domain.PdsMonitorTask;
import com.pds.web.monitor.domain.PdsMonitorTaskActivity;
import com.pds.web.monitor.mapper.PdsMonitorTaskActivityMapper;
import com.pds.web.monitor.mapper.PdsMonitorTaskMapper;
import com.pds.web.monitor.service.IPdsMonitorTaskActivityService;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class PdsMonitorTaskActivityServiceImpl extends ServiceImpl<PdsMonitorTaskActivityMapper, PdsMonitorTaskActivity> implements IPdsMonitorTaskActivityService {

    @Resource
    private PdsMonitorTaskActivityMapper pdsMonitorTaskActivityMapper;
    @Resource
    private PdsMonitorTaskMapper pdsMonitorTaskMapper;
    @Resource
    private PdsTestOrganizationInfoMapper pdsTestOrganizationInfoMapper;

    /**
     * 新增
     *
     * @param pdsMonitorTaskActivity
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public PdsMonitorTaskActivity addActivity(PdsMonitorTaskActivity pdsMonitorTaskActivity) {
        Date date = DateUtils.parseDate(DateUtils.getTime());

        if (pdsMonitorTaskActivity.getActivityDate() == null) {
            pdsMonitorTaskActivity.setActivityDate(date);
        }

        pdsMonitorTaskActivity.setStatus("1");

        pdsMonitorTaskActivity.setCreateTime(date);

        pdsMonitorTaskActivityMapper.addActivity(pdsMonitorTaskActivity);

        QueryWrapper<PdsMonitorTask> monitorTaskWrapper = new QueryWrapper<>();
        monitorTaskWrapper.eq("id", pdsMonitorTaskActivity.getTaskId());
        PdsMonitorTask pdsMonitorTask = pdsMonitorTaskMapper.selectOne(monitorTaskWrapper);

        QueryWrapper<PdsTestOrganizationInfo> testOrganizationInfoWrapper = new QueryWrapper<>();
        testOrganizationInfoWrapper.eq("id",pdsMonitorTask.getOrganizationId());
        PdsTestOrganizationInfo pdsTestOrganizationInfo = pdsTestOrganizationInfoMapper.selectOne(testOrganizationInfoWrapper);

        String monitorCountYear = pdsTestOrganizationInfo.getMonitorCountYear();
        BigDecimal oldMonitorCountYear = new BigDecimal(monitorCountYear);
        BigDecimal newMonitorCountYear = oldMonitorCountYear.add(new BigDecimal(1));
        pdsTestOrganizationInfo.setMonitorCountYear(newMonitorCountYear.toString());

        pdsTestOrganizationInfoMapper.updateById(pdsTestOrganizationInfo);

        return pdsMonitorTaskActivity;
    }

    /**
     * 删除 -- 根据id
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean deleteActivity(Long id) {
        return pdsMonitorTaskActivityMapper.deleteActivity(id) > 0;
    }

    /**
     * 更新
     *
     * @param pdsMonitorTaskActivity
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updateActivity(PdsMonitorTaskActivity pdsMonitorTaskActivity) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorTaskActivity.setUpdateTime(date);

        return pdsMonitorTaskActivityMapper.updateActivity(pdsMonitorTaskActivity) > 0;
    }

    /**
     * 查询 -- 分页/条件：taskId，activityName，activityDate
     *
     * @param pdsMonitorTaskActivity
     * @return
     */
    @Override
    public List<PdsMonitorTaskActivity> selectActivityList(PdsMonitorTaskActivity pdsMonitorTaskActivity) {
        return pdsMonitorTaskActivityMapper.selectActivityList(pdsMonitorTaskActivity);
    }
}
